local skynet = require "skynet"
local skynet_managet = require "skynet.manager"
local log = require "sky_log"
local room_pb_list = require "room_pb_list"

local service_room_manager = {}

function service_room_manager:init()
    --根据配置创建好房间(不同类型等级的副本)
    self.room_list = {}
    self.game_id = skynet.getenv("game_id")
    self.node_id = skynet.getenv("node_id")

    local room_config = require "test_room_config"
    for _ , v in pairs(room_config) do
        local room = skynet.newservice("service_room")
        self.room_list[v.room_id] = {}
        self.room_list[v.room_id].room_id = v.room_id
        self.room_list[v.room_id].player_ids = {}
        self.room_list[v.room_id].player_nums = 0
        self.room_list[v.room_id].room_agent = room

        skynet.send(room, "lua", "init_room", v)
    end
    log.debug("self.room_list=", self.room_list)
end

function service_room_manager:enter_room(agent, player_info)
    log.debug("player_info=", player_info)
    player_info.agent = agent
    self.room_list[player_info.room_id].player_ids[player_info.player_id] = player_info
    self.room_list[player_info.room_id].player_nums = self.room_list[player_info.room_id].player_nums + 1
    skynet.send(self.room_list[player_info.room_id].room_agent, "lua", "enter_room", agent, player_info)
    log.debug("self.room_list=", self.room_list)

    if player_info.table_id then
        self:enter_game(agent, player_info.player_id)
    end
end

function service_room_manager:enter_game(agent, player_id)
    log.debug("self.room=", self.room_list)
    local room_agent
    local player_info
    for _, v in pairs(self.room_list) do
        if v.player_ids[player_id] then 
            room_agent = v.room_agent
            player_info = v.player_ids[player_id]
        end
    end

    if room_agent and player_info then 
        skynet.send(room_agent, "lua", "enter_game", agent, player_info, "10001-25001-1")
    end
end

function service_room_manager:disconnect(agent, player_id, room_id, table_id)
    local the_room_id = room_id
    if not the_room_id then 
        for id, room_info in pairs(self.room_list) do
            if room_info.player_ids[player_id] then 
                the_room_id = id 
            end
        end
    end 

    if not the_room_id then 
        log.error("disconnect no room_id!")
        return
    end

    self.room_list[the_room_id].player_ids[player_id] = nil
    self.room_list[the_room_id].player_nums = self.room_list[the_room_id].player_nums - 1
    skynet.send(self.room_list[the_room_id].room_agent, "lua", "disconnect", player_id, table_id)
    log.debug("self.room_list=", self.room_list)
end

function service_room_manager:handle_msg(agent, pack_id, msg_title, msg_data)
    log.debug("service_room_manager:handle_msg ! msg_title=", msg_title)
    log.debug("service_room_manager:handle_msg ! msg_data=", msg_data)
    local handler = require (room_pb_list[pack_id])
    handler[msg_title](handler, agent, msg_data)
end 

--start
skynet.start(function()
    --session是当前服务地址  address是源服务(发消息来的服务)地址
    skynet.dispatch("lua", function(session, address, cmd, ...)
        log.info("service_room_manager dispatch cmd=", cmd)
        log.info("service_room_manager dispatch session=", session)
        log.info("service_room_manager dispatch address=", address)
        local f = service_room_manager[cmd]
        if f then
            skynet.retpack( f(service_room_manager,...) )
        else
            error(string.format("Unknown command %s", tostring(cmd)))
        end
    end)

    skynet.register(".service_room_manager")
    service_room_manager:init()
end)


return service_room_manager